
    /**  
    * @Title: LogStorageMongoService.java
    * @Package com.efast.cafe.commponent.log.service.impl
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年8月31日
    * @version V1.0  
    */
    
package com.efast.cafe.commponent.log.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Sort.Order;
import com.efast.cafe.commponent.log.bean.LogInfoQueryBean;
import com.efast.cafe.commponent.log.constant.LogConstant;
import com.efast.cafe.commponent.log.entity.BusLogInfo;
import com.efast.cafe.commponent.log.service.LogStorageService;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.util.StringUtil;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort;
/**
    * @ClassName: LogStorageMongoService
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年8月31日
    *
    */
@ConditionalOnProperty(
        name=LogConstant.K_STORAGE,
        havingValue="mongodb"
)
@Service("logStorageMongoService")
public class LogStorageMongoServiceImpl implements LogStorageService {
	
	private static String collectionName = "bus_log_info";
	
	
	@Autowired
	@Qualifier("logMongoTemplate")
	private MongoTemplate mongoTemplate;

	/* (非 Javadoc)
	* 
	* 
	* @param logInfoBean
	* @see com.efast.cafe.commponent.log.service.LogStorageService#saveLog(com.efast.cafe.commponent.log.entity.LogInfo)
	*/

	@Override
	public void saveLog(BusLogInfo logInfoBean) {
		// TODO Auto-generated method stub
		mongoTemplate.insert(logInfoBean,collectionName);
	}

	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @param logInfoQueryBean
	    * @return
	    * @see com.efast.cafe.commponent.log.service.LogStorageService#queryPage(com.efast.cafe.commponent.log.bean.LogInfoQueryBean)
	    */
	    
	@Override
	public QueryPageBase queryPage(LogInfoQueryBean logInfoQueryBean) {
		
		List<Criteria> criterias = new ArrayList<>();
		if (StringUtil.isNotBlank(logInfoQueryBean.getCompanyCode())) {
			criterias.add(Criteria.where("companyCode").is(logInfoQueryBean.getCompanyCode()));
		}
		if (StringUtil.isNotBlank(logInfoQueryBean.getLevel())) {
			criterias.add(Criteria.where("level").is(logInfoQueryBean.getLevel()));
		}
		if (StringUtil.isNotBlank(logInfoQueryBean.getSystemNo())) {
			criterias.add(Criteria.where("systemNo").is(logInfoQueryBean.getSystemNo()));
		}
		if (StringUtil.isNotBlank(logInfoQueryBean.getSystemVersion())) {
			criterias.add(Criteria.where("systemVersion").is(logInfoQueryBean.getSystemVersion()));
		}
		if (StringUtil.isNotBlank(logInfoQueryBean.getUserName())) {
			criterias.add(Criteria.where("userName").is(logInfoQueryBean.getUserName()));
		}
		if (StringUtil.isNotBlank(logInfoQueryBean.getRemark())) {
			Pattern pattern = Pattern.compile("^.*"+logInfoQueryBean.getRemark()+".*$", Pattern.CASE_INSENSITIVE);
			criterias.add(Criteria.where("remark").regex(pattern));
		}
		if (StringUtil.isNotBlank(logInfoQueryBean.getMsg())) {
			Pattern pattern = Pattern.compile("^.*"+logInfoQueryBean.getMsg()+".*$", Pattern.CASE_INSENSITIVE);
			criterias.add(Criteria.where("msg").regex(pattern));
		}
		return page(logInfoQueryBean,criterias.toArray(new Criteria[0]));
	}
	
	private QueryPageBase page(QueryPageBase page, Criteria... criterias) {
		
		List<Criteria> list = new ArrayList<Criteria>();
		if (null != criterias) {
			for (Criteria criteria : criterias) {
				list.add(criteria);
			}
		}

		Query query = new Query();
		if (list.size() > 0) {
			query.addCriteria(new Criteria().andOperator(list.toArray(new Criteria[0])));
		}
		long totalRows = mongoTemplate.count(query, collectionName);
		page.setTotalRows((int) totalRows);
		{
			query.skip(page.getStart());
			query.limit(page.getLength());
		}
		{
			List<Order> orders = new ArrayList<>();
			Order e = new Order(Direction.DESC, "createTime");
			orders.add(e);
			if (orders.size() > 0) {
				query.with(new Sort(orders));
			}
		}

		if (query.getSortObject() == null) {
			query.with(new Sort(Direction.DESC, "_id"));
		}
		List<LogInfoQueryBean> content = mongoTemplate.find(query, LogInfoQueryBean.class, collectionName);
		page.setResultList(content);
		return page;
	}


	
}
